package com.xique.door.project.xyl.service.impl;

import com.github.pagehelper.PageInfo;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.dao.*;
import com.xique.door.bean.request.RentStatisticRequest;
import com.xique.door.bean.vo.xyl.*;
import com.xique.door.project.xyl.service.IXylPopulationService;
import com.xique.door.service.IAreaService;
import com.xique.door.service.IBuildBuildingService;
import com.xique.door.service.IBuildHouseService;
import com.xique.door.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2021/12/22 13:55
 */
@Service
public class XylPopulationServiceImpl implements IXylPopulationService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IBuildHouseService houseService;

    @Autowired
    private IBuildBuildingService buildingService;

    @Override
    public AjaxResult domicileStatistic(Long parkId) {

        List<DomicileStatisticDAO> daos = userService.selectUserDomicileStatisticByProvince(parkId);
        List<Long> provinceIds = daos.stream().map(DomicileStatisticDAO::getProvinceId).collect(Collectors.toList());
        List<DomicileStatisticVO> list = OrikaUtil.converts(daos, DomicileStatisticVO.class);

        Map<Long, String> areaMap = areaService.selectAreaName(provinceIds);

        list.forEach(item -> {
            if (StringUtils.isNotEmpty(areaMap.get(item.getProvinceId()))) {
                item.setName(areaMap.get(item.getProvinceId()));
            }else {
                item.setName("其他");
            }
        });

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult ageStatistic(Long parkId) {

        List<AgeStatisticDAO> daos = userService.selectUserAgeStatistic(parkId);

        List<AgeStatisticVO> list = OrikaUtil.converts(daos, AgeStatisticVO.class);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult tenantStatistic(Long parkId) {

        List<TenantStatisticDAO> daos = houseService.selectTenantStatisticByBuilding(parkId);
        List<Long> buildingIds = daos.stream().map(TenantStatisticDAO::getBuildingId).collect(Collectors.toList());
        Map<Long, String> buildingMap = buildingService.selectBuildingName(buildingIds);

        List<TenantStatisticVO> list = OrikaUtil.converts(daos, TenantStatisticVO.class);
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(buildingMap.get(item.getBuildingId()))) {
                item.setBuildingName(buildingMap.get(item.getBuildingId()));
            }
        });

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult houseStatistic(Long parkId) {

        List<HouseStatisticDAO> daos = houseService.selectHouseStatisticByBuilding(parkId);
        List<Long> buildingIds = daos.stream().map(HouseStatisticDAO::getBuildingId).collect(Collectors.toList());
        Map<Long, String> buildingMap = buildingService.selectBuildingName(buildingIds);

        List<HouseStatisticVO> list = OrikaUtil.converts(daos, HouseStatisticVO.class);
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(buildingMap.get(item.getBuildingId()))) {
                item.setBuildingName(buildingMap.get(item.getBuildingId()));
            }
        });

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult rentStatistic(RentStatisticRequest request) {

        Long parkId = SecurityUtils.getParkId();
        request.setParkId(parkId);

        List<RentUserStatisticDAO> daos = houseService.selectRentUserStatistic(request);
        List<Long> buildingIds = daos.stream().map(RentUserStatisticDAO::getBuildingId).distinct().collect(Collectors.toList());
        Map<Long, String> buildingMap = buildingService.selectBuildingName(buildingIds);

        List<Long> houseIds = daos.stream().map(RentUserStatisticDAO::getHouseId).distinct().collect(Collectors.toList());
        List<RentHouseUserCountStatisticDAO> rentHouseUserCountStatisticDAOS = houseService.selectRentHouseUserCountStatisticByHouse(houseIds);
        Map<Long, Integer> rentHouseUserCountStatisticMap = rentHouseUserCountStatisticDAOS.stream().collect(Collectors.toMap(RentHouseUserCountStatisticDAO::getHouseId, RentHouseUserCountStatisticDAO::getUserCount));

        List<Long> provinceIds = daos.stream().map(RentUserStatisticDAO::getProvinceId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
        List<Long> cityIds = daos.stream().map(RentUserStatisticDAO::getCityId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
        provinceIds.addAll(cityIds);
        Map<Long, String> areaMap = areaService.selectAreaName(provinceIds);

        List<RentUserStatisticVO> list = OrikaUtil.converts(daos, RentUserStatisticVO.class);
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(buildingMap.get(item.getBuildingId()))) {
                item.setBuildingName(buildingMap.get(item.getBuildingId()));
            }
            if (StringUtils.isNotNull(rentHouseUserCountStatisticMap.get(item.getHouseId()))) {
                if (rentHouseUserCountStatisticMap.get(item.getHouseId()) > 1) {
                    item.setRentType(2);
                    item.setRentTypeName("合租");
                } else {
                    item.setRentType(1);
                    item.setRentTypeName("整租");
                }
            }
            if (StringUtils.isNotEmpty(areaMap.get(item.getProvinceId()))) {
                item.setProvince(areaMap.get(item.getProvinceId()));
            }
            if (StringUtils.isNotEmpty(areaMap.get(item.getCityId()))) {
                item.setCity(areaMap.get(item.getCityId()));
            }
        });

        long total = new PageInfo(daos).getTotal();
        Map<String, Object> map = new HashMap<>(2);
        map.put("list", list);
        map.put("total", total);

        return AjaxResult.success(map);
    }
}
